package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwaoT;
import cn.e4j.modular.crm.entity.GwaolT;
import cn.e4j.modular.crm.mapper.GwaoTMapper;
import cn.e4j.modular.crm.service.GwaoTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yuhuan
 * @since 2019-01-10
 */
@Service
public class GwaoTServiceImpl extends ServiceImpl<GwaoTMapper, GwaoT> implements GwaoTService {

    @Autowired
    GwaoTMapper gwaoTMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwahDtos = this.baseMapper.listPage(page, dto);

        return gwahDtos;
    }

    @Override
    public GwaoT getByKey(String GWAO001) {
        return this.baseMapper.getByKey(GWAO001);
    }

    /**
     * 增加
     *
     * @param gwaoT,gwaolT
     * @return
     */
    @Transactional
    public void add(GwaoT gwaoT, GwaolT gwaolT) {
        gwaoT.insert();
        gwaolT.insert();
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @Transactional
    public void edit(GwaoT gwaoT, GwaolT gwaolT) {
        UpdateWrapper<GwaoT> wrapper = new UpdateWrapper<>();
        wrapper.eq("GWAOENT", gwaoT.getGWAOENT());
        wrapper.eq("GWAO001", gwaoT.getGWAO001());

        UpdateWrapper<GwaolT> gwaolTwrapper = new UpdateWrapper<>();
        gwaolTwrapper.eq("GWAOLENT", gwaoT.getGWAOENT());
        gwaolTwrapper.eq("GWAOL001", gwaoT.getGWAO001());
        gwaoT.update(wrapper);
        gwaolT.update(gwaolTwrapper);
    }

    /**
     * 删除
     *
     * @param dto
     * @return
     */
    @Transactional
    public void del(Dto dto) {
        gwaoTMapper.deleteGwao(dto);
        gwaoTMapper.deleteGwaol(dto);
    }

    @Transactional
    public void deleteDataBatch(List<GwaoT> gwaoTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwaoT gwaoT : gwaoTList) {
            Dto inDto = Dtos.newDto();
            inDto.put("GWAOENT", user.getDBC());
            inDto.put("GWAO001", gwaoT.getGWAO001());
            gwaoTMapper.deleteGwao(inDto);
            gwaoTMapper.deleteGwaol(inDto);
        }
    }
}
